﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

using System.Windows.Media.Imaging;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.IO;
using BootyShaker.Serialization;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
namespace BootyShaker
{

    public class Shaker
    {
        public WriteableBitmap thumbnail { get; set; }

        public double DampingRatio { get; set; }
        public double Frequency { get; set; }
        public double GravityRatio  { get; set; }
        public double DragStrength { get; set; }
        public bool MergeOverlay { get; set; }

        public Version version;

        public bool isNew = true;
        public bool isLoad = false;
        public string filename;
        [DataMember]
        public WriteableBitmap shakeImage { get; set; }
        [DataMember]
        public Point shakeImagePosition { get; set; }



        //public CompositeTransform transform
        //{
        //    set
        //    {
        //        ScaleX = value.ScaleX;
        //        ScaleY = value.ScaleY;
        //        CenterX = value.CenterX;
        //        CenterY = value.CenterY;
        //        Rotation = value.Rotation;
        //        TranslateX = value.TranslateX;
        //        TranslateY = value.TranslateY;
        //    }

        //}

        //[DataMember]
        //public double ScaleX { get; set; }
        //[DataMember]
        //public double ScaleY { get; set; }
        //[DataMember]
        //public double CenterX { get; set; }
        //[DataMember]
        //public double CenterY { get; set; }
        //[DataMember]
        //public double Rotation { get; set; }
        //[DataMember]
        //public double TranslateX { get; set; }
        //[DataMember]
        //public double TranslateY { get; set; }
        [DataMember]
        public Matrix matrix { get; set; }
        [DataMember]
        public List<ShakeObject> shakeObjects { get; set; }

        public List<ShakeObjectCtl> shakeObj
        {
            set
            {
                shakeObjects = new List<ShakeObject>();

                foreach (ShakeObjectCtl obj in value)
                {
                   
                    Point tran = obj.RenderTransform.Transform(new Point(0, 0));
                    Point DesignLocation = new Point(tran.X, tran.Y);
                    double wdt = obj.Width * ((CompositeTransform)obj.RenderTransform).ScaleX;
                    double hgt = obj.Height * ((CompositeTransform)obj.RenderTransform).ScaleY;
                    shakeObjects.Add(new ShakeObject(DesignLocation, new Size(wdt, hgt)));
                    //shakeObjects.Add(new ShakeObject(obj.DesignLocation, new Size(obj.Width, obj.Height)));
                }
            }
        }

        public Shaker()
        {
            shakeObjects = new List<ShakeObject>();

        }

        public Shaker(WriteableBitmap image, Point position, Matrix trans, List<ShakeObjectCtl> objects)
        {

            shakeImage = image;
            shakeImagePosition = position;
            //ScaleX = trans.ScaleX;
            //ScaleY = trans.ScaleY;
            //CenterX = trans.CenterX;
            //CenterY = trans.CenterY;
            //Rotation = trans.Rotation;
            //TranslateX = trans.TranslateX;
            //TranslateY = trans.TranslateY;
            matrix = trans;
            shakeObj = objects;
        }

        public Shaker(WriteableBitmap image, WriteableBitmap thumb, Point position, Matrix trans, List<ShakeObjectCtl> objects)
            : this(image, position, trans, objects)
        {
            thumbnail = thumb;
        }

        public void Save(Stream strm)
        {

            BinarySerializationHelper.Serialize(strm, this);

            strm.Close();

           
           
            App.LoadNeeded = true;

        }
        public static Shaker Load(string filename)
        {
            var myStore = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream myFileStream = myStore.OpenFile(filename, FileMode.Open);
            try
            {



                // deserialization
                var sampleData = BinarySerializationHelper.Deserialize(myFileStream, typeof(Shaker));
                ((Shaker)sampleData).isNew = false;
                ((Shaker)sampleData).isLoad = true;
                ((Shaker)sampleData).filename = filename;
                return (Shaker)sampleData;
            }
            finally
            {
                myFileStream.Close();
            }
        }

        public static void GetPreviewImage(string filename,bool force = false)
        {
            using (IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!myStore.FileExists(App.thumbPath + "\\" + Path.GetFileNameWithoutExtension(filename) + ".jpg") || force)
                {
                    IsolatedStorageFileStream myFileStream = myStore.OpenFile(filename, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(myFileStream);
                    try
                    {
                        // deserialization

                        CustomBinarySerializer.GetThumbnail(br, filename);

                    }
                    catch (Exception ex) 
                    {
                    }
                    finally
                    {
                        br.Close();
                        myFileStream.Close();
                    }
                }
            }

        }
    }
}
